package com.dmzc.stack;

import java.util.Stack;

/***
 *7.使用Stack实现queue
 */
public class ImplementQueueUseStack {

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        System.out.println(queue.peek());
    }
}

class MyQueue {
    /**
     * Initialize your data structure here.
     */
    private final Stack<Integer> inStack;
    private final Stack<Integer> outStack;

    public MyQueue() {
        this.inStack = new Stack<>();
        this.outStack = new Stack<>();
    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        this.inStack.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        if (this.outStack.isEmpty()) {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
            if (outStack.isEmpty())
                throw new RuntimeException("no element");
        }
        return outStack.pop();
    }

    /**
     * Get the front element.
     */
    public int peek() {
        if (this.outStack.isEmpty()) {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
            if (outStack.isEmpty())
                throw new RuntimeException("no element");
        }
        return outStack.peek();


    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }
}
